package com.shuqi.activity.viewport;

import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.controller.R;

/**
 * 加载Dialog 成功、失败Dialog
 * 
 * @author chenwei
 * 
 */
public class ToastDialog {

    private BottomDialog dialog;

    private View view;

    private TextView tvMsg;

    private ImageView ivLoading;
    private ImageView ivFail;
    private ImageView ivSuccess;

    private Context context;

    public ToastDialog(Context context) {
        this.context = context;

        view = View.inflate(context, R.layout.view_dialog_toast, null);

        tvMsg = (TextView) view.findViewById(R.id.tv_msg);

        ivLoading = (ImageView) view.findViewById(R.id.iv_loading);
        ivFail = (ImageView) view.findViewById(R.id.iv_fail);
        ivSuccess = (ImageView) view.findViewById(R.id.iv_success);

        dialog = new BottomDialog(context, view);
        dialog.setCanceledOnTouchOutside(false);
    }

    public void loading(String loadingMsg) {
        ivLoading.setVisibility(View.VISIBLE);
        ivFail.setVisibility(View.GONE);
        ivSuccess.setVisibility(View.GONE);

        Animation operatingAnim = AnimationUtils.loadAnimation(context, R.anim.dialog_loading);

        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);

        ivLoading.startAnimation(operatingAnim);

        if (loadingMsg != null && !"".equals(loadingMsg)) {
            tvMsg.setText(loadingMsg);
        }

        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void loading() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void show(boolean isSuccess, String loadingMsg) {
        ivLoading.setVisibility(View.GONE);
        ivLoading.clearAnimation();
        if (isSuccess) {
            ivFail.setVisibility(View.GONE);
            ivSuccess.setVisibility(View.VISIBLE);

        } else {
            ivFail.setVisibility(View.VISIBLE);
            ivSuccess.setVisibility(View.GONE);
        }

        if (loadingMsg != null && !"".equals(loadingMsg)) {
            tvMsg.setText(loadingMsg);
        }

        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void setDissmisOnClickOutSide(boolean isCan) {
        if (dialog != null) {
            dialog.setDissmisOnClickOutSide(isCan);
        }
    }

    public void setOnCancelListener(OnCancelListener l) {
        if (dialog != null) {
            dialog.setOnCancelListener(l);
        }
    }
    public boolean isShowing(){
        return dialog==null?false:dialog.isShowing();
    }
}
